import Vue from 'vue'
import VueRouter from 'vue-router'
import {setUser,getUser,removeUser} from '@/api/session';
Vue.use(VueRouter)


const routes = [
  {
	  path: '/',//首页
	  name: 'index',
      component: () => import('@/views/Index'),
      children: [
        {
          path: '/user',
          name: 'User',
          component: () => import('@/views/User')
        }
      ]
   },
   {
	  path: '/login',//登录 
	  name: 'login',
	  component: () => import('@/views/Login')
   } 
]

const router = new VueRouter({
	// mode: 'history', 
	base: 'admin',
	scrollBehavior: () => ({ y: 0 }),
	routes
})

//判断登录
router.beforeEach((to, from, next) => {
    console.log(to.path)
    to.path === '/login'
    next();
    if(to.path === '/login'){
        removeUser();
        next();
    }else{
        var user = getUser();
        if(!user){
            next({
                path: '/login'
            })
        }
    }
  })

export default router
